package com.atguigu.service.impl;

import com.atguigu.Bean.C_book;
import com.atguigu.Bean.C_page;
import com.atguigu.dao.BookDao;
import com.atguigu.dao.impl.BookDaoImpl;
import com.atguigu.service.BookService;

import java.util.List;

public class BookServiceImpl implements BookService {
    private BookDao bd=new BookDaoImpl();
    @Override
    public void addBook(C_book book) {
        int i = bd.addbook(book);
        if(i==1){
            System.out.println("添加成功");
        }
    }

    @Override
    public void deleteBookBYId(int id) {
        int i = bd.deleteBookById(id);
        if(i==1){
            System.out.println("删除成功");
        }
    }

    @Override
    public void updateBook(C_book book) {
        int i = bd.updateBook(book);
        if(i==1){
            System.out.println("修改成功");
        }

    }

    @Override
    public C_book queryBookById(int id) {
        return bd.queryBookById(id);
    }

    @Override
    public List<C_book> queryBooks() {
        return bd.queryBooks();
    }

    @Override
    public C_page<C_book> page(int pageNo, int pageSize) {
        C_page<C_book> C_page=new C_page<>();

        //设置每页显示数量
        C_page.setPageSize(pageSize);
        //求并设置总记录数
        Integer pageTotalCount= bd.queryForPageTotalCount();
        C_page.setPageTotalCount(pageTotalCount);
        //设置总页码数
        Integer pageTotal=pageTotalCount/pageSize;
        if(pageTotalCount%pageSize!=0){
            pageTotal++;
        }
        C_page.setPageTotal(pageTotal);
        //设置当前页码
        C_page.setPageNo(pageNo);
        //设置当前页数据
        int begin=(C_page.getPageNo()-1)*pageSize;
        List<C_book> books=bd.queryForPageItems(begin,pageSize);
        C_page.setItems(books);
        return C_page;
    }

    @Override
    public C_page<C_book> pageByPrice(int pageNo, int pageSize, int min, int max) {
        C_page<C_book> C_page=new C_page<>();

        //设置每页显示数量
        C_page.setPageSize(pageSize);
        //求并设置总记录数
        Integer pageTotalCount= bd.queryForPageTotalCountByPrice(min,max);
        C_page.setPageTotalCount(pageTotalCount);
        //设置总页码数
        Integer pageTotal=pageTotalCount/pageSize;
        if(pageTotalCount%pageSize!=0){
            pageTotal++;
        }
        C_page.setPageTotal(pageTotal);
        //设置当前页码
        C_page.setPageNo(pageNo);
        //设置当前页数据
        int begin=(C_page.getPageNo()-1)*pageSize;
        List<C_book> books=bd.queryForPageItemsByPrice(begin,pageSize,min,max);
        C_page.setItems(books);
        return C_page;
    }
}
